#!/usr/bin/php -q
<?php
   set_time_limit(30);
   require('phpagimihira.php');
   require('sqlmihira.php');
   error_reporting(E_ALL);
     
   $agi = new AGI();
   $agi->answer();

   
   $cid = $agi->parse_callerid();
   $agi->verbose('Extension................');
   $agi->verbose($agi->request['agi_extension']);
   $direction=$argv[1];
   $db = new AGIDB($agi);
   
   
   
   //============================Configurations=======================================================
   $db->dbname = 'classified';
   $db->dbuser = 'root';
   $db->dbpass = 'PASSWORD1'; 
   $landsparentid=2;
   $vehiclesparentid=1;
   $subscriptionvalidity=30;
   $suggestingradius=10;
   //============================Configurations=======================================================
   
 
   $msisdn=$direction=='out'?$argv[2]:str_replace("+", "", $cid['username']);
   
   
   
   function increase_clasification_rank($itemstring){
   		global $db;
   		$itemstring=str_replace("|", ",", $itemstring); 		
   		$res = $db->sql("update `classification` set `rank`=`rank`+1 where `itemid` in (".$itemstring.")","NONE");	
   	
   }
   
   function getparents($itemid){

   	global $db;   	
   	$parent=$itemid;
   	while(true){
   		$res = $db->sql("SELECT `parentid` from `classification` where `itemid`='".$itemid."'", "ASSOC");    // get prompt parent
   		if ($res[0]['parentid']<=10){
   			break;
   		}
   		else{
   			$parent=$res[0]['parentid'].'|'.$parent;
   			$itemid=$res[0]['parentid'];
   		}
   	}
   	
   	return $parent;
   	
   	
   }
  
   function get_classified_prompt($itemid)
   {
		global $db;
        $res = $db->sql("SELECT concat('classification/',`prompt`) as `prompt` from `classification` where `itemid`=".$itemid, "ASSOC");    // get prompt name
        return $res[0]['prompt'];

   }

   function charge_customer($msisdn, $chargevent, $description){
   	
   		return 1;			// to be removed when charging implemented
	   	$ch = curl_init();
	   	curl_setopt($ch, CURLOPT_URL, "http://10.1.80.21:8080/cgi-bin/erl/omniecg_web/chrg_sub?ContentProvider=APALYA&MSISDN=".$msisdn."&SERVICECODE=".$chargevent."&Description=Optional");
	   	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	   	$output = curl_exec($ch);
	   	curl_close($ch);
	   	
	   	if ($output==1 || $output==0)
	   		return $output;
	   	else
	   		return -1;   	
	   	  	
   }
   
   function send_sms($destination, $origination, $msg){   	
	   	$ch = curl_init();
	   	curl_setopt($ch, CURLOPT_URL, "http://localhost:8970/cgi-bin/sendsms?username=jjjjj&password=lllll&from=".$origination."&to=".$destination."&text=".$msg);
	   	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	   	$output = curl_exec($ch);
	   	curl_close($ch);
	   	 
	   	return $output;
   }
   
   function subscribe_customer($msisdn,$servicetype,$serviceid)
   {
		global $agi,$db,$promptroot,$subscriptionvalidity;
		$agi->verbose("caling subscribe customer.............",1);	
	 	
	 	$serviceitem=getparents($serviceid);
	 	$res = $db->sql("SELECT `msisdn`, `servicetype`,`serviceitem`,`status` from `subscriptions` where `serviceitem`=".$serviceid." and `msisdn`='".$msisdn."'", "ASSOC"); // chk for any previous subscriptions
	 	$agi->verbose(var_export($res,true));
	 	if (sizeof($res)==0){
	 		
	 		 $chargeres=charge_customer($msisdn, "SUBSCRIPTION", $servicetype);	// Charge the customer
	 		 if($chargeres==1){
	 		 	//$res = $db->sql("INSERT INTO `subscriptions`(`msisdn`, `servicetype`,`serviceitem`,`status`,`expiredate`) VALUES ('".$msisdn."','".$servicetype."','".$serviceitem."','active','".date('Y-m-d', strtotime("+".$subscriptionvalidity." days"))."') ", "ASSOC");    // insert the subscription
	 		 	$res=null;
	 		 	$res = $db->sql("insert into subscriptions SELECT null as `subscriptionid`, CURRENT_TIMESTAMP as `activatedtime`,'".$msisdn."' as `msisdn`, '".$servicetype."' as `servicetype`,".$serviceid." as `serviceitem` ,'ACTIVE' as `status`, null as `lastalertsenttime`, '".date('Y-m-d', strtotime("+".$subscriptionvalidity." days"))."' as `expiredate`, max(id)as `startalertid`,max(id)as `lastalertid`, 0 as `alertcount` FROM `alertstatus` where `delivered`=1 ", "NONE");    // insert the subscription
	 		 	
 		 		increase_clasification_rank($serviceitem);
 		 		$agi->stream_file($promptroot.'subscriptionactivated', '');               // play the sucessfully completed prompt
 		 		$selection=-1;
 		 		$selection=$agi->play_menu($promptroot.$servicetype.'postsubscriptionmenu','123',3);
 		 		return $selection;
	 		 	
	 		 	
	 		 }
	 		 else if($chargeres==0){	// insufficient balance
	 		 	$agi->stream_file($promptroot.'subscriptionfailedinsuff', '');
	 		 	$agi->hangup();
	 		 }
	 		 else {		// TEmp Error
	 		 	$agi->stream_file($promptroot.'subscriptionfailedtemperror', '');
	 		 	$agi->hangup();
	 		 }
	 		
	 		
	 	}
	 	else{
	 		$agi->stream_file($promptroot.'subscribedalready', '');		// Already Activated
	 		$selection=-1;
	 		$selection=$agi->play_menu($promptroot.$servicetype.'postsubscriptionmenu','123',3);
	 		return $selection;
	 	}
	 	
   }

   function deleteadvert($msisdn,$advertid,$filename)
   {
        global $agi,$db,$promptroot;
        $res = $db->sql("delete from `adverts` where `advertid`=".$advertid);    // delete the subscription
		$output = shell_exec("rm -f /var/lib/asterisk/sounds/classifiedadverts/".$filename.".gsm");
		$agi->verbose("Deeleting File.............".$filename,1);
        $agi->stream_file($promptroot.'advertdeleted', '');               // play the sucessfully deleted the advert

   }

   function deactivate_advert($msisdn,$advertid)
   {
        global $agi,$db,$promptroot;       
		$res = $db->sql("update `adverts` set `status`='deactivated' where `advertid`=".$advertid);    // delete the subscription 
        $agi->stream_file($promptroot.'advertdeactivated','');             // play the sucessfully deleted the advert

   }

   function activateadvert($msisdn,$advertid)
   {
        global $agi,$db,$promptroot;
        $res = $db->sql("update adverts set `status`='active' where `advertid`=".$advertid);    // delete the subscription
        $agi->stream_file($promptroot.'advertactivated', '');               // play the sucessfully deleted the advert

   }

   function deletesubscription($msisdn,$subscriptionid)
   {
        global $agi,$db,$promptroot;
        $res = $db->sql("delete from subscriptions where `subscriptionid`=".$subscriptionid);    // delete the subscription
        $agi->stream_file($promptroot.'subscriptiondeleted','');             // play the sucessfully deleted the Subscription

   }

   function deactivatesubscription($msisdn,$subscriptionid)
   {
        global $agi,$db,$promptroot;
		$res = $db->sql("update subscriptions set `status`='deactivated' where `subscriptionid`=".$subscriptionid);    //  ctivatedsubscription
        $agi->stream_file($promptroot.'subscriptiondeactivated','');             // play the sucessfully deactivated the Subscription

   }

   function activatesubscription($msisdn,$subscriptionid)
   {
        global $agi,$db,$promptroot;
        $res = $db->sql("update subscriptions set `status`='active' where `subscriptionid`=".$subscriptionid);    // delete the subscription
        $agi->stream_file($promptroot.'subscriptionactivated','');             // play the sucessfully activated the Subscription
	
   }

   function manage_registrations($registrationtype,$servicetype="'lands','vehicles'")
   {

	    ManageRegistrations:
		global $agi,$db,$msisdn,$language,$promptroot;
		$agi->verbose(var_export($registrationtype,true));
	
		$counter=0;
	    $promptlist=null;	
		if($registrationtype['unprocessedadverts'])
		{
			$unprocessedadverts = $db->sql("SELECT `advertid`,concat('/var/lib/asterisk/sounds/classifiedadverts/',`promptname`) as `promptname` FROM `adverts` WHERE `msisdn`='".$msisdn."' and `status`='unprocessed' and `servicetype` in (".$servicetype.") order by `entrytime`", "ASSOC");
			$agi->verbose(var_export($unprocessedadverts,true));
			if (sizeof($unprocessedadverts)>0)
	                {
	                	$promptlist=array(array('prompt'=>'unprocessedadverts', 'name'=>'unprocessedadverts','list'=>$unprocessedadverts));
	                        $counter++;
	                }
		}
		if($registrationtype['activeadverts'])
	    {
		        $activeadverts = $db->sql("SELECT `advertid`,concat('/var/lib/asterisk/sounds/classifiedadverts/',`promptname`) as `promptname` FROM `adverts` WHERE `msisdn`='".$msisdn."' and `status`='active' and `servicetype` in (".$servicetype.")  order by `entrytime`", "ASSOC");
                if (sizeof($activeadverts)>0)
                {
	                if (is_null($promptlist))
        	                $promptlist=array(array('prompt'=>'activeadverts', 'name'=>'activeadverts','list'=>$activeadverts));
                        else
                                array_push($promptlist,array('prompt'=>'activeadverts', 'name'=>'activeadverts','list'=>$activeadverts));

                        $counter++;
                }
		}
		if($registrationtype['inactiveadverts'])
        {
	        $inactiveadverts = $db->sql("SELECT `advertid`,concat('/var/lib/asterisk/sounds/classifiedadverts/',`promptname`) as `promptname` FROM `adverts` WHERE `msisdn`='".$msisdn."' and `status` in ('inactive','deactivated') and `servicetype` in (".$servicetype.") order by `entrytime`", "ASSOC");
        	if (sizeof($inactiveadverts)>0)
	        {
        	        if (is_null($promptlist))
                	        $promptlist=array(array('prompt'=>'inactiveadverts', 'name'=>'inactiveadverts','list'=>$inactiveadverts));
	                else
        	                array_push($promptlist,array('prompt'=>'inactiveadverts', 'name'=>'inactiveadverts','list'=>$inactiveadverts));

                	$counter++;
        	}
		}
        if($registrationtype['activesubscriptions'])
        {
                $activesubscriptions = $db->sql("SELECT `subscriptionid`,`servicetype`,`serviceitem` FROM `subscriptions`  WHERE `msisdn`='".$msisdn."' and `status` ='active' and `servicetype` in (".$servicetype.") order by `activatedtime`", "ASSOC");
                if (sizeof($activesubscriptions)>0)
                {
                        if (is_null($promptlist))
                                $promptlist=array(array('prompt'=>'activesubscriptions', 'name'=>'activesubscriptions','list'=>$activesubscriptions));
                        else
                                array_push($promptlist,array('prompt'=>'activesubscriptions', 'name'=>'activesubscriptions','list'=>$activesubscriptions));

                        $counter++;
                }
        }
        if($registrationtype['inactivesubscriptions'])
        {
                $inactivesubscriptions = $db->sql("SELECT `subscriptionid`,`servicetype`,`serviceitem` FROM `subscriptions`  WHERE `msisdn`='".$msisdn."' and `status` in ('inactive','deactivated') and servicetype in (".$servicetype.") order by activatedtime", "ASSOC");
                if (sizeof($inactivesubscriptions)>0)
                {
                        if (is_null($promptlist))
                                $promptlist=array(array('prompt'=>'inactivesubscriptions', 'name'=>'inactivesubscriptions','list'=>$inactivesubscriptions));
                        else
                                array_push($promptlist,array('prompt'=>'inactivesubscriptions', 'name'=>'inactivesubscriptions','list'=>$inactivesubscriptions));

                        $counter++;
                }
        }
        if ($counter==0)
        {
                 return 97;	//no registrations
        }
        else
        {
	        
				$agi->verbose(var_export($promptlist,true));
				$selection=-1;
				$selection=$agi->play_dynamic_menu($promptroot,$promptlist); 
				if($selection==99)      // go back
	        	{
	                	return 99;
		        }
	        	elseif($selection==98)          // go to main
		        {
	        	        return 98;
		        }
	        	else            // item selected
		        {
		
					if ($promptlist[$selection]['name']=='unprocessedadverts')
					{
						$advertlist=$promptlist[$selection]['list'];
						$count=0;
						while ($count<sizeof($advertlist))
						{
							$playlist=array($promptroot.'advertno',$promptroot.'digits/'.($count+1), $advertlist[$count]['promptname']);
							$agi->stream_concatenated_prompts($playlist,'');
							if($count==sizeof($advertlist)-1)
							{
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageunprocessedadvertsmenu2','12');
							}
							else
							{
								
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageunprocessedadvertsmenu1','123');
							}
		
							if($selection=='1')      // delete this advert
					        {
	        	                	deleteadvert($msisdn,$advertlist[$count]['advertid'],$advertlist[$count]['promptname']);
									return 98;
					        }
				        	elseif($selection=='2'&& $count==0)          // go to previous menu
					        {
					                goto ManageRegistrations;
					        }
					        elseif($selection=='2')          // go back to the previous item
				        	{
								$count--;
							}
	        	            elseif($selection=='3')          // go to the next item
	                	    {
	                        	$count++;
	                        }
		
						}
					
					}
					elseif($promptlist[$selection]['name']=='activeadverts')
					{
						$advertlist=$promptlist[$selection]['list'];
                	    $count=0;
                        while ($count<sizeof($advertlist))
	                    {
        	            	$playlist=array($promptroot.'advertno',$promptroot.'digits/'.($count+1), $advertlist[$count]['promptname']);
                	        $agi->stream_concatenated_prompts($playlist,'');
                        	if($count==sizeof($advertlist)-1)
                        	{
                        		$selection=-1;
                        		$selection=$agi->play_menu($promptroot.'manageactiveadvertsmenu2','12');
                        	}
	                        else{
	                        	$selection=-1;
	                        	$selection=$agi->play_menu($promptroot.'manageactiveadvertsmenu1','123');
	                        }

                	        if($selection=='1')      // delete this advert
                        	{
                            	deactivate_advert($msisdn,$advertlist[$count]['advertid']);                                       
								return 98;
	                        }
        	                elseif($selection=='2'&& $count==0)          // go to previous menu
                	        {
                        	     goto ManageRegistrations;
                	        }
	                        elseif($selection=='2')          // go back to the previous item
        	                {
                	             $count--;
                        	}
                            elseif($selection=='3')          // go to the next item
	                        {
        	                     $count++;
                	        }
						}
	
					}
					elseif($promptlist[$selection]['name']=='inactiveadverts')
					{
							$advertlist=$promptlist[$selection]['list'];
	                        $count=0;
        	                while ($count<sizeof($advertlist))
                	        {
                        	        $playlist=array($promptroot.'advertno',$promptroot.'digits/'.($count+1), $advertlist[$count]['promptname']);
                                	$agi->stream_concatenated_prompts($playlist,'');
	                                if($count==sizeof($advertlist)-1)
	                                {
	                                	$selection=-1;
	                                	$selection=$agi->play_menu($promptroot.'manageinactiveadvertsmenu2','12');
	                                }
                	                else
                	                {
                	                	$selection=-1;
                	                	$selection=$agi->play_menu($promptroot.'manageinactiveadvertsmenu1','123');
                	                }
	
        	                        if($selection=='1')      // Activate this advert
                	                {
                        	                activateadvert($msisdn,$advertlist[$count]['advertid']);
											return 98;
	                                }
									elseif($selection=='2')      // delete this advert
                                    {
                                    	deleteadvert($msisdn,$advertlist[$count]['advertid'],$advertlist[$count]['promptname']);
                                        return 98;
                                    }
        	                        elseif($selection=='3'&& $count==0)          // go to previous menu
                	                {
                        	                goto ManageRegistrations;
                                	}
	                                elseif($selection=='3')          // go back to the previous item
        	                        {
                	                        $count--;
                        	        }
                                	elseif($selection=='4')          // go to the next item
	                                {
        	                                $count++;
									}
							}
					}			
                        
					elseif($promptlist[$selection]['name']=='activesubscriptions')
					{
						$subscriptionlist=$promptlist[$selection]['list'];
						$count=0;
						while ($count<sizeof($subscriptionlist))
						{
							$parents=explode("|", getparents($subscriptionlist[$count]['serviceitem']));
							$agi->verbose($subscriptionlist[$count]['serviceitem'] ,true);
							if ($subscriptionlist[$count]['servicetype']=='lands' && $language=='eng')
							{
								
								$playlistcomplete=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part2',$promptroot.get_classified_prompt($parents[sizeof($parents)-1]),$promptroot.'playsubscription-lands-part3',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlistdistrict=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlist=sizeof($parents)==1?$playlistdistrict:$playlistcomplete;
					
							}
							elseif ($subscriptionlist[$count]['servicetype']=='vehicles'  && $language=='eng')
							{
								//$playlist=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-vehicles-part1',$promptroot.get_classified_prompt($parents[1]),$promptroot.get_classified_prompt($parents[2]),$promptroot.get_classified_prompt($parents[0]));
								//$playlist=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-vehicles-part1',sizeof($parents)>=3?$promptroot.get_classified_prompt($parents[sizeof($parents)-1]):"",$promptroot.get_classified_prompt($parents[0]),sizeof($parents)>1?$promptroot.'playsubscription-vehicles-part2':"",sizeof($parents)>1?$promptroot.get_classified_prompt($parents[1]):"");
								$playlist=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-vehicles-part1',sizeof($parents)>=3?$promptroot.get_classified_prompt($parents[sizeof($parents)-1]):"",sizeof($parents)>=3?$promptroot.'playsubscription-vehicles-part2':"",sizeof($parents)==1?$promptroot.get_classified_prompt($parents[0]):$promptroot.get_classified_prompt($parents[1]));
								
							}
							elseif ($subscriptionlist[$count]['servicetype']=='lands' && $language=='sin')
							{
								// need to be corrected								
								$playlistcomplete=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part2',$promptroot.get_classified_prompt($parents[sizeof($parents)-1]),$promptroot.'playsubscription-lands-part3',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlistdistrict=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlist=sizeof($parents)==1?$playlistdistrict:$playlistcomplete;
					
							}
							elseif ($subscriptionlist[$count]['servicetype']=='vehicles'  && $language=='sin')
							{
								// need to be corrected
								$playlist=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-vehicles-part1',sizeof($parents)>1?$promptroot.get_classified_prompt($parents[1]):$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-vehicles-part2', sizeof($parents)>3?$promptroot.get_classified_prompt($parents[sizeof($parents)-1]):"",$promptroot.'playsubscription-vehicles-part3');
													
							}
							// vehicle : you have subscribed to receive SMS alerts on toyota 110 cars. manufactured year, above 2012
							// Lands : you have subscribed to receive SMS alerts on lands arround homagama area in Colombo district. land size bellow 20 prchs
					
					
							$agi->stream_concatenated_prompts($playlist,'');
							if($count==sizeof($subscriptionlist)-1)
							{
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageactivesubscriptions2','12');
							}
							else 
							{
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageactivesubscriptions1','123');
							}
					
							if($selection=='1')      // delete this subscription
							{
								deactivatesubscription($msisdn,$subscriptionlist[$count]['subscriptionid']);
								return 98;
							}
							elseif($selection=='2'&& $count==0)          // go to previous menu
							{
								goto ManageRegistrations;
							}
							elseif($selection=='2')          // go back to the previous item
							{
								$count--;
							}
							elseif($selection=='3')          // go to the next item
							{
								$count++;
							}
						}
					}					
					
					elseif($promptlist[$selection]['name']=='inactivesubscriptions')
					{
						$subscriptionlist=$promptlist[$selection]['list'];
						$count=0;
						while ($count<sizeof($subscriptionlist))
						{
							$parents=explode("|", getparents($subscriptionlist[$count]['serviceitem']));
							$agi->verbose($subscriptionlist[$count]['serviceitem'] ,true);
							if ($subscriptionlist[$count]['servicetype']=='lands' && $language=='eng')
							{
								
								$playlistcomplete=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part2',$promptroot.get_classified_prompt($parents[sizeof($parents)-1]),$promptroot.'playsubscription-lands-part3',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlistdistrict=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlist=sizeof($parents)==1?$playlistdistrict:$playlistcomplete;
					
							}
							elseif ($subscriptionlist[$count]['servicetype']=='vehicles'  && $language=='eng')
							{
								$playlist=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-vehicles-part1',$promptroot.get_classified_prompt($parents[1]),$promptroot.get_classified_prompt($parents[2]),$promptroot.get_classified_prompt($parents[0]));
								
							}
							elseif ($subscriptionlist[$count]['servicetype']=='lands' && $language=='sin')
							{
								// need to be corrected
								
								$playlistcomplete=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part2',$promptroot.get_classified_prompt($parents[sizeof($parents)-1]),$promptroot.'playsubscription-lands-part3',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlistdistrict=array($promptroot.'subscriptionno',$promptroot.'digits/'.($count+1), $promptroot.'playsubscription-lands-part1',$promptroot.'playsubscription-lands-part4',$promptroot.get_classified_prompt($parents[0]),$promptroot.'playsubscription-lands-part5');
								$playlist=sizeof($parents)==1?$playlistdistrict:$playlistcomplete;
					
							}
							elseif ($subscriptionlist[$count]['servicetype']=='vehicles'  && $language=='sin')
							{
								// need to be corrected
								$playlist=array('subscriptionno','digits/'.($count+1), 'playsubscription-vehicles-part1',get_classified_prompt($parents[1]),get_classified_prompt($parents[2]),$subscriptionlist[$count]['prompt'],get_classified_prompt($parents[0]),'playsubscription-vehicles-part2');
					
					
							}
							// vehicle : you have subscribed to receive SMS alerts on toyota 110 cars. manufactured year, above 2012
							// Lands : you have subscribed to receive SMS alerts on lands arround homagama area in Colombo district. land size bellow 20 prchs
					
					
							$agi->stream_concatenated_prompts($playlist,'');
							if($count==sizeof($subscriptionlist)-1)
							{
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageinactivesubscriptions2','12');
							}
							else 
							{
								$selection=-1;
								$selection=$agi->play_menu($promptroot.'manageinactivesubscriptions1','123');
							}
					
							if($selection=='1')      // activate this subscription
							{
								activatesubscription($msisdn,$subscriptionlist[$count]['subscriptionid']);
								return 98;
							}
							elseif($selection=='2')      // delete this subscription
							{
								deletesubscription($msisdn,$subscriptionlist[$count]['subscriptionid']);
								return 98;
							}
							elseif($selection=='3'&& $count==0)          // go to previous menu
							{
								goto ManageRegistrations;
							}
							elseif($selection=='3')          // go back to the previous item
							{
								$count--;
							}
							elseif($selection=='4')          // go to the next item
							{
								$count++;
							}
						}
					}
					
				}
					
		}	

   }

   function set_language()
   {
		global $agi,$db,$msisdn,$language,$promptroot;
		$selection=-1;
        $selection=$agi->play_menu($promptroot.'selectlanguage','123',3);
        if ($selection=='1')     // sinhala
        {
                $language='sin';	// change this to 'sin'
                $agi->exec_setlanguage($language);   // set the language
        }
        elseif ($selection=='4')     // tamil
        {
                $language='eng';
                $agi->exec_setlanguage($language);   // set the language
        }
        else      // English
        {
                $language='eng';
                $agi->exec_setlanguage($language);   // set the language
        }  
   }

    $language='eng';
    $agi->exec_setlanguage($language);
    $promptroot='classified/';
    

 //  $language='en/classified';	// to be removed 
 // $agi->exec_setlanguage($language);	// to be removed

   Home:
   set_language();		

   $res=null;
   
   $input=$agi->get_data('enterparent',3000,5);
   
   $agi->verbose(var_export($input,true));
   
   if (intval($input['result'])>0)
   {
   		$res = $db->sql("SELECT `itemid`,`name`,concat('classification/',`prompt`) as `prompt` from `classification` where `parentid`=".intval($input['result'])." order by `itemid`", "ASSOC");    // select the level 1 items
   					
   }
   else{
   	$res = $db->sql("SELECT `itemid`,`name`,concat('classification/',`prompt`) as `prompt` from `classification`  order by `itemid` ", "ASSOC");    // select the level 1 items
   }
   $selection=$agi->play_dynamic_menu($promptroot,$res);	// play the level's dynamic menu
   				
   
   $agi->hangup();
   
    
?>


